<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>

        <img src="~/assets/images/logo.png" alt=""/>
        <span>智慧社区后台管理系统</span>


      </div>
      <div>
        <div class="souSuo">
          <el-select v-model="comId" @change="localComId" placeholder="请选择">
            <el-option
                v-for="item in communityAllName"
                :key="item.communityId"
                :label="item.communityName"
                :value="item.communityId">
            </el-option>
          </el-select>
        </div>
        <div id="user-name">欢迎您：{{ this.username }}</div>
        <full-screen/>
        <el-dropdown trigger="click">
          <span class="el-dropdown-link d-flex">
               <img v-if="imageUrl" :src="imageUrl" id="user-avatar">
               <img v-if="!imageUrl" src="~assets/images/avatar.gif" id="user-avatar">
            <i class="el-icon-arrow-down el-icon--right"></i>
          </span>
          <el-dropdown-menu slot="dropdown">
            <!--            <el-dropdown-item @click.native="toggleRouter">WelCome</el-dropdown-item>-->
            <!--            <a target="_blank" href="https://gitee.com/penggang-home/shopping-system">-->
            <!--              <el-dropdown-item>Gitee</el-dropdown-item>-->
            <!--            </a>-->
            <!--            <a href="https://www.bookbook.cc/vue2/shopping-system-v2/" target="_blank">-->
            <!--              <el-dropdown-item>V: 1.2.0</el-dropdown-item>-->
            <!--            </a>-->
            <el-dropdown-item divided @click.native="toPersonalCenter">个人中心</el-dropdown-item>

            <el-dropdown-item divided @click.native="open">退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </el-header>

    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏菜单 -->
      <el-aside :width="isCollapse ? '64px' : '200px'" style="overflow-y: hidden">
        <!-- 切换按钮 -->
        <div class="toggle-button" @click="toggleCollapse()">
          {{ isCollapse ? '&rArr;' : '&lArr;' }}
        </div>

        <el-menu
            :collapse="isCollapse"
            :collapse-transition="false"
            background-color="#333744"
            ref="elMenuRef"
            text-color="#fff"
            active-text-color="#409EFF"
            :router="true"
            :default-active="activePath"
            :unique-opened="true"
        >
          <el-menu-item index="/welcome">
            <i class="iconfont icon-home3"></i>
            <span slot="title">WelCome</span>
          </el-menu-item>


          <!-- 一级菜单 -->
          <el-submenu id="item.menuId + ''" :index="item.menuId + ''" v-for="item in sysMenusTree" :key="item.menuId">
            <!-- 一级菜单模板区域 -->
            <template slot="title">
              <i :class="item.icon"></i>
              <span>{{ item.menuName }}</span>
            </template>
            <template v-for="itemChild in item.childSysMenusByUsername">
              <!-- 二级菜单没有三级菜单 -->
              <el-menu-item-group :key="itemChild.menuId" v-if="itemChild.childSysMenusByUsername == null">
                <el-menu-item :index="itemChild.path">
                  <i :class="itemChild.icon"></i>
                  <span>{{ itemChild.menuName }}</span>
                </el-menu-item>
              </el-menu-item-group>

              <!-- 二级菜单中有三级菜单 -->
              <el-submenu v-if="itemChild.childSysMenusByUsername != null" :index="itemChild.path"
                          :key="itemChild.menuId">
                <template slot="title">
                  <i :class="itemChild.icon"></i>
                  <span>{{ itemChild.menuName }}</span>
                </template>
                <!-- 三级菜单 -->
                <el-menu-item v-for="itemChild_under in itemChild.childSysMenusByUsername" :index="itemChild_under.path"
                              :key="itemChild_under.menuId">
                  <template slot="title">
                    <i :class="itemChild_under.icon"></i>
                    <span>{{ itemChild_under.menuName }}</span>
                  </template>
                </el-menu-item>
              </el-submenu>
            </template>
          </el-submenu>


          <el-menu-item @click="jumpToDataView">
            <i class="iconfont icon-baobiao"></i>
            <span slot="title">数据视图</span>
            <i class="el-submenu__icon-arrow icon-share"></i>
          </el-menu-item>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主体 -->
      <el-main>
        <!-- 匹配到的子路由 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import FullScreen from 'components/common/FullScreen/'
import {MenuItemGroup} from 'element-ui';

export default {
  name: 'Home',
  data() {
    return {
      url: "",
      //图片存储地址
      imageUrl: '',
      //登录的用户名
      username: "",
      //根据用户名查询的权限集合
      sysMenusTree: [],
      // 左侧菜单数据
      // menuList: [],
      //
      // iconsObj: {
      //   125: 'iconfont icon-user',
      //   103: 'iconfont icon-tijikongjian',
      //   101: 'iconfont icon-shangpin',
      //   102: 'iconfont icon-danju',
      //   145: 'iconfont icon-baobiao', //数据统计模块的icon
      // },
      //控制左侧边栏的切换状态
      isCollapse: false,
      // 被激活的链接地址
      activePath: '/welcome',
      // 需要关闭的左侧菜单id
      // menusId: [],

      //小区名字全局查询
      communityAllName: [],
      comId: localStorage.getItem("comId"),
    }
  },
  components: {
    FullScreen,
    'el-menu-item-group': MenuItemGroup,
  },
  created() {
    // this.getMenuList()
  },
  watch: {
    // 监听路由的变化
    $route: {
      handler(val, oldVal) {
        this.activePath = val.path
        // this.menusId.forEach(item => {
        //   // 关闭所有左侧菜单
        //   this.$refs.elMenuRef.closeMenu(item + '')
        // })
      },
      // 深度观察监听
      deep: true,
    },
  },
  methods: {
    jumpToDataView() {
      window.open('http://www.bookbook.cc/vue2/data-view/#/home')
    },
    open() {
      this.$confirm('确定注销并退出系统吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.logout();
      })
    },
    // 退出
    logout() {
      this.username = null;
      this.$router.push('/login')
    },
    //根据用户名读取权限信息
    loadPermissions() {
      this.username = localStorage.getItem("username");
      this.$axios.get("/sysUser/sysUser-sysMenus/" + this.username)
          .then(result => {
            if (result.data.code == 200) {
              this.sysMenusTree = result.data.data;
            } else {
              return this.$message.error({
                message: result.data.message,
                duration: 1000,
              });
            }
          })
    },
    //跳转个人中心页面
    toPersonalCenter() {
      // this.username = localStorage.getItem("username");
      // this.$axios.get("/sysUser/personalInformation/" + this.username)
      //     .then(result => {
      //       console.log(result.data.data)
      //       if (result.data.code == 200) {
      this.$router.push("/personal");
      //   } else {
      //     return this.$message.error({
      //       message: result.data.message,
      //       duration: 1000,
      //     });
      //   }
      // })
    },
    // 点击按钮切换菜单的折叠与咱开
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 跳转到欢迎页面
    toggleRouter() {
      this.$router.push('/welcome')
    },
    //小区名字全局搜索框
    selectCommunityAllName() {
      this.$axios.get("/zyCommunity/communitys")
          .then(result => {
            if (result.data.code == 200) {
              console.log(result.data.data)
              this.communityAllName = result.data.data;
            }
          })
    },
    localComId() {
      localStorage.setItem("comId", this.comId);
      console.log(localStorage.getItem("comId"));
      location.reload();
    }
  },

  mounted() {
    this.loadPermissions();
    this.selectCommunityAllName();
    this.$axios.get("/sysUser/personalInformation/" + this.username)
        .then(result => {
          console.log(result.data.data)
          if (result.data.code == 200) {
            this.personalInformation = result.data.data;
            this.imageUrl = this.personalInformation.avatar;
          } else {
            return this.$message.error({
              message: result.data.message,
              duration: 2000,
            });
          }
        })
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}

.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;

  div {
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: white;
    font-size: 22px;
    font-weight: bold;

    #version {
      font-weight: normal;
      font-size: 16px;
      margin-left: 20px;
    }

    img {
      width: 58px;
      height: 58px;
      border-radius: 50%;
      margin-right: 20px;
    }
  }
}

.el-aside {
  background-color: #333744;
}

.el-main {
  position: relative;
  background-color: white;
}

.el-menu {
  border-right: none;
}

.iconfont {
  margin-right: 10px;
}

.toggle-button {
  background-color: #4a5064;
  color: white;
  font-size: 22px;
  line-height: 24px;
  text-align: center;
  letter-spacing: 0.1em;
  cursor: pointer;
  user-select: none;
}

.d-flex {
  display: flex;
  justify-content: center;
  align-items: center;
}

#user-avatar {
  width: 45px;
  height: 45px;
  border-radius: 10px;
  margin-right: 0;
}

a {
  text-decoration: none;
}

#user-name {
  font-weight: normal;
  margin-right: 25px;
  font-size: 20px;
}
.souSuo{
  margin-right: 20px;
}
</style>
